<?php
namespace tool;


use ZPHP\Core\Log;

class AesCode
{
    private $iv = "0988454384fedcba";//密钥偏移量IV，可自定义
    private $encryptKey = "azsqed1234567890";//AESkey，可自定义
    /**
     *解密参数
     * @param
     * @return string
     */
    public function decode($encryptStr)
    {
      $localIV = $this->iv;
      $encryptKey = $this->encryptKey;
        $encryptedData = base64_decode($encryptStr);
      $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $encryptKey, $encryptedData, MCRYPT_MODE_CBC, $localIV);
      return trim($decrypted);
    }
    /**
    *加密参数
    * @param $params string
    * @return string
    */
    public function encode($encryptStr)
    {
      $localIV = $this->iv;
      $encryptKey = $this->encryptKey;
      $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $encryptKey, $encryptStr, MCRYPT_MODE_CBC, $localIV);
      return base64_encode($encrypted);
    }


    /**
    *生成签名
    *@param  $app_key,$timestamp,$params   string
    *@return string
    */

    public function createSign($app_key,$timestamp,$params)
    {
        //生成签名
        $_sign = md5(trim($app_key.$timestamp.$params));
        return $_sign;
    }

    //校验签名

    /**
     * @param $app_key
     * @param $timestamp
     * @param $params
     * @param $sign
     * @return bool
     */
    public function verify($app_key,$timestamp,$params,$sign)
    {
        //1.生成签名
        $_sign = $this->createSign($app_key,$timestamp,$params);
        //2.签名校验
        if(strcasecmp($_sign,$sign) != 0)
        {
          return false;
        }
          return true;
    }
}
